package com.example.sjjg.utest.view

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ObservableArrayMap
import androidx.fragment.app.Fragment
import com.example.sjjg.utest.R
import com.example.sjjg.utest.databinding.FragmentMainBinding

class MainFragment : Fragment() {
    var lifeCycle       = -1
    var map             = ObservableArrayMap<String,Any>()
    lateinit var binding: FragmentMainBinding

    fun onDataBtnClicked(v : View){
        parentFragmentManager.beginTransaction().add(R.id.root,DataFragment()).addToBackStack("DataFrgmt").commit()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        lifeCycle = 0
    }
    override fun onDetach() {
        super.onDetach()
        lifeCycle = -1
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifeCycle = 1
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        lifeCycle = 2
        binding = FragmentMainBinding.inflate(inflater)
        binding.dataBtn.setOnClickListener(this::onDataBtnClicked)
        map.apply {
            val args = arguments
            put("name",args?.getString("name"))
            put("class",args?.getString("class"))
            put("age",args?.getInt("age").toString())
            put("email",args?.getString("email"))
        }
        binding.map = map
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        lifeCycle = 3
    }
    override fun onDestroyView() {
        super.onDestroyView()
        lifeCycle = 8
    }
    override fun onDestroy() {
        super.onDestroy()
        lifeCycle = 9
    }
    override fun onStart() {
        super.onStart()
        lifeCycle = 4
    }
    override fun onResume() {
        super.onResume()
        lifeCycle = 5
    }
    override fun onPause() {
        super.onPause()
        lifeCycle = 6
    }
    override fun onStop() {
        super.onStop()
        lifeCycle = 7
    }
}